package cn.com.xuhx.testMapper;

import cn.com.xuhx.mapper.ReturnTypeMapper;
import cn.com.xuhx.pojo.Department;
import cn.com.xuhx.pojo.Equipment;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * 返回测试
 */
public class ReturnTypeTest {

    private SqlSession sqlSession = null;

    @Before
    public void getSQLSession() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //设置需要手动提交事务
        //sqlSession = build.openSession();
        //设置自动提交事务
        sqlSession = build.openSession(true);
    }

    @After
    public void closeSQLSession(){
        sqlSession.clearCache();
        sqlSession.close();
    }

    /**
     * 一个参数映射，返回map类型
     */
    @Test
    public void testQueryMap(){
        ReturnTypeMapper mapper = sqlSession.getMapper(ReturnTypeMapper.class);
        Map<String,Object> user = mapper.queryUserByIDForMap(2);
        System.out.println(user);
    }



    /**
     * 多个参数映射（命名参数）
     */
    @Test
    public void testQueryMoreparamMap2(){
        ReturnTypeMapper mapper = sqlSession.getMapper(ReturnTypeMapper.class);
        List<Map<String,Object>> users = mapper.queryUserByAgeAndTelMap(23, "18115188222");
        System.out.println(users);
    }

    /**
     * 多对一（级联属性）
     */
    @Test
    public void testDdY(){
        ReturnTypeMapper mapper = sqlSession.getMapper(ReturnTypeMapper.class);
        List<Equipment> equipments = mapper.queryEquipment();
        System.out.println(equipments);
    }

    /**
     * 多对一（association）
     */
    @Test
    public void testDdY2(){
        ReturnTypeMapper mapper = sqlSession.getMapper(ReturnTypeMapper.class);
        List<Equipment> equipments = mapper.queryEquipment2();
        System.out.println(equipments);
    }

    /**
     * 多对一（association分步查询）
     */
    @Test
    public void testDdY3(){
        ReturnTypeMapper mapper = sqlSession.getMapper(ReturnTypeMapper.class);
        List<Equipment> equipments = mapper.queryEquipment3();
        //System.out.println(equipments);
        System.out.println(equipments.get(0).getSex());//测试延迟加载配置
    }

    /**
     * 一对多(collection)
     */
    @Test
    public void testYDD(){
        ReturnTypeMapper mapper = sqlSession.getMapper(ReturnTypeMapper.class);
        List<Department> departments = mapper.queryDepartment();
        System.out.println(departments);
    }

    /**
     * 一对多（collection分步查询）
     */
    @Test
    public void testYDD2(){
        ReturnTypeMapper mapper = sqlSession.getMapper(ReturnTypeMapper.class);
        List<Department> departments = mapper.queryDepartment2();
        System.out.println(departments);
    }
}
